/*Required packages: -blindschemes-, -texsave-, -outreg-, -grc1leg-
net install blindschemes, from(http://fmwww.bc.edu/RePEc/bocode/b/)
net install texsave,from(http://fmwww.bc.edu/RePEc/bocode/t/)
net install outreg,from(http://fmwww.bc.edu/RePEc/bocode/o/)
net install grc1leg, from(http://www.stata.com/users/vwiggins)
*/

*clear
clear all
*Version
version 17
*Graph Scheme
set scheme plottig 

/*Set Working directory
cd xxxx
*/


***********************************
****Read in data/Prepare Frames****
***********************************

*Frame: replication_main
use "./data/scp_replication.dta", clear
bys id: gen numofwaves = _N
lab var numofwaves "Number of waves with valid responses"
gen balanced = 1 if numofwaves == 20
replace balanced = 0 if numofwaves < 20
lab var balanced "Responded in all 20 waves"
egen WaveNum = group(wave)
lab var WaveNum "Wave number, no gabs"
xtset id WaveNum
frame put *, into(replication_main)


*Frame: replication_ess
use "./data/ess_replication",clear
keep if essround>4
tostring essround, gen(eround)
gen name = con+"-R"+eround
frame put *,into(replication_ess)

***********************************************
********Main and supplementary analyses********
***********************************************
frame copy replication_main temp,replace
cwf temp
sort id WaveNum

*Model 1: RE
xtreg socialtrust l.poltrust if balanced==1, re cluster(id)
mat a = 1,r(table)[1,1],r(table)[2,1],r(table)[4,1]

xtreg poltrust l.socialtrust if balanced==1, re cluster(id)
mat a = a,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', `e(N)'

xtreg socialtrust l.poltrust , re cluster(id)
mat b = 1,r(table)[1,1],r(table)[2,1],r(table)[4,1]

xtreg poltrust l.socialtrust , re cluster(id)
mat b = b,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', e(N)'


*Model 2: 1-way FE
xtreg socialtrust l.poltrust if balanced==1, fe cluster(id)
mat aa = 2,r(table)[1,1],r(table)[2,1],r(table)[4,1]
xtreg poltrust l.socialtrust if balanced==1, fe cluster(id)
mat aa = aa,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', `e(N)'
mat a = a\aa
xtreg socialtrust l.poltrust, fe cluster(id)
mat bb = 2,r(table)[1,1],r(table)[2,1],r(table)[4,1]
xtreg poltrust l.socialtrust, fe cluster(id)
mat bb = bb,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', `e(N)'
mat b = b\bb

*Model 3: 2-way FE
xtreg socialtrust l.poltrust i.WaveNum if balanced==1, fe cluster(id)
mat aa = 3,r(table)[1,1],r(table)[2,1],r(table)[4,1]
xtreg poltrust l.socialtrust i.WaveNum if balanced==1, fe cluster(id)
mat aa = aa,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', `e(N)'
mat a = a\aa
xtreg socialtrust l.poltrust i.WaveNum, fe cluster(id)
mat bb = 3,r(table)[1,1],r(table)[2,1],r(table)[4,1]
xtreg poltrust l.socialtrust i.WaveNum, fe cluster(id)
mat bb = bb,r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)', `e(N)'
mat b = b\bb

*model 4: SEM
keep id poltrust socialtrust WaveNum
reshape wide poltrust socialtrust, i(id) j(WaveNum)
sem (poltrust2 <- poltrust1@b1 socialtrust1@b2 Alpha1@1 ) ///
    (poltrust3 <- poltrust2@b1 socialtrust2@b2 Alpha1@1 ) ///
    (poltrust4 <- poltrust3@b1 socialtrust3@b2 Alpha1@1 ) ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (poltrust18 <- poltrust17@b1 socialtrust17@b2 Alpha1@1 ) ///
    (poltrust19 <- poltrust18@b1 socialtrust18@b2 Alpha1@1 ) ///
    (poltrust20 <- poltrust19@b1 socialtrust19@b2 Alpha1@1 ) ///
    (socialtrust2 <- socialtrust1@b3 poltrust1@b4 Alpha2@1 ) ///
	 (socialtrust3 <- socialtrust2@b3 poltrust2@b4 Alpha2@1 ) ///
    (socialtrust4 <- socialtrust3@b3 poltrust3@b4 Alpha2@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
    (socialtrust18 <- socialtrust17@b3 poltrust17@b4 Alpha2@1 ) ///
    (socialtrust19 <- socialtrust18@b3 poltrust18@b4 Alpha2@1 ) ///
    (socialtrust20 <- socialtrust19@b3 poltrust19@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog  ///
	  cov(socialtrust1*poltrust1 socialtrust1*Alpha2 poltrust1*Alpha1 Alpha1*Alpha2) 

*For Table 1
mat aa = 4,r(table)[1,9],r(table)[2,9],r(table)[4,9]
mat aa = aa,r(table)[1,2],r(table)[2,2],r(table)[4,2],`e(N)',`e(N)'
mat a = a\aa
*For table H1
mat ap = r(table)[1,9],r(table)[2,9],r(table)[4,9],r(table)[1,1],r(table)[2,1],r(table)[4,1]\r(table)[1,10],r(table)[2,10],r(table)[4,10],r(table)[1,2],r(table)[2,2],r(table)[4,2] 

*Different coefficient?
qui:{
	sem (poltrust2 <- poltrust1@b1 socialtrust1@b2 Alpha1@1 ) ///
    (poltrust3 <- poltrust2@b1 socialtrust2@b2 Alpha1@1 ) ///
    (poltrust4 <- poltrust3@b1 socialtrust3@b2 Alpha1@1 ) ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (poltrust18 <- poltrust17@b1 socialtrust17@b2 Alpha1@1 ) ///
    (poltrust19 <- poltrust18@b1 socialtrust18@b2 Alpha1@1 ) ///
    (poltrust20 <- poltrust19@b1 socialtrust19@b2 Alpha1@1 ) ///
    (socialtrust2 <- socialtrust1@b3 poltrust1@b4 Alpha2@1 ) ///
	 (socialtrust3 <- socialtrust2@b3 poltrust2@b4 Alpha2@1 ) ///
    (socialtrust4 <- socialtrust3@b3 poltrust3@b4 Alpha2@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
    (socialtrust18 <- socialtrust17@b3 poltrust17@b4 Alpha2@1 ) ///
    (socialtrust19 <- socialtrust18@b3 poltrust18@b4 Alpha2@1 ) ///
    (socialtrust20 <- socialtrust19@b3 poltrust19@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog vce(cluster id) ///
	  cov(socialtrust1*poltrust1 socialtrust1*Alpha2 poltrust1*Alpha1 Alpha1*Alpha2) 
}
test _b[poltrust2:socialtrust1] = _b[socialtrust2:poltrust1] 
loc semst = _b[poltrust2:socialtrust1]
loc sempt = _b[socialtrust2:poltrust1] 
 capture putpdf clear
 putpdf begin 
 putpdf paragraph
 putpdf text ("Different coefficients?"),bold linebreak
 putpdf text ("Is `semst'  and  `sempt' significantly different?") ,linebreak
 putpdf text ("chi2(`r(df)') = `r(chi2)'"),linebreak
 putpdf text ("Prob > chi2 = `r(p)'")
 putpdf save "./tables/Different coefficients" , replace 
***xtabond
frame copy replication_main temp,replace
cwf temp
sort id WaveNum
xtabond socialtrust l.poltrust if balanced==1
mat aa = r(table)[1,2],r(table)[2,2],r(table)[4,2]\r(table)[1,1],r(table)[2,1],r(table)[4,1]
xtabond poltrust l.socialtrust if balanced==1
mat bb = r(table)[1,1],r(table)[2,1],r(table)[4,1],`e(N_g)',`e(N)'\r(table)[1,2],r(table)[2,2],r(table)[4,2],`e(N_g)',`e(N)'
mat xtabond = aa,bb


****Table 1**********
mat T1 = a[1..4,2..3], a[1..4,5..6]
local bc = rowsof(a)
matrix stars = J(`bc',4,0)
forvalues k = 1/`bc' {
	 matrix stars[`k',1] = (a[`k',4]<0.001)+(a[`k',4]<0.01)+(a[`k',4]<0.05)
	 matrix stars[`k',3] = (a[`k',7]<0.001)+(a[`k',7]<0.01)+(a[`k',7]<0.05)
}

local bc = rowsof(a)
forvalues k = 1/`bc' {
loc io`k'1 = a[`k',8]
loc io`k'2 = a[`k',9]
}

mat T1 = a[1..4,2..7]
mat list T1
frmttable using "./tables/Table1",statmat(T1) sdec(3) replace rtitle(1,RE  \ 2,One-way FE \ 3,Two-way FE  \ 4,DPM ) ctitle(Model, Estimator, A,"", "",B,"",""\ "" , "" , DV: Social trust, "", "", DV: Political trust, "",""\"","",Coef.,SE,p value,Coef.,SE,p value) multicol(1,3,3;1,6,3; 2,3,3;2,6,3) addcols("A and B"\N\"Individuals/observations"\ "`io11'/`io12'" \ "`io21'/`io22'" \ "`io31'/`io32'" \ "`io41'/`io42'")

*Appendix - Full Table 1*
frmttable using "./tables/TableA1",statmat(ap) sdec(3)  rtitle(IV: PolTrust (lagged) \ IV: SocTrust (lagged)) ctitle( "" , DV: Social trust,"","", DV: Political trust,"","") replace tex note(Note: Coefficients with standard errors clustered on individuals in parenthes\and p values in brackets (two-sided tests)\N = `io1' individuals/`io2' observations) fragment coljust(l;c)  basefont(fs10) multicol(1,2,3;1,5,3)


*Appendix - Table xtabond*
loc io1 = xtabond[1,8]
loc io2 = xtabond[1,9]
mat xta = xtabond[1..2,1..6]
frmttable using "./tables/Tablexta",statmat(xta) sdec(3)  rtitle(IV: PolTrust (lagged) \ IV: SocTrust (lagged)) ctitle( "" , DV: Social trust, ,"","",DV: Political trust,"","") replace tex note(Note: Coefficients with standard errors and p values in brackets (two-sided tests)\N = `io1' individuals/`io2' observations) fragment multicol(1,2,3;1,5,3)


*Appendix Model 1-3*
mat T3 = b[1..3,2..7]

local bc = rowsof(b)
forvalues k = 1/`bc' {
loc io`k'1 = b[`k',8]
loc io`k'2 = b[`k',9]
}
frmttable using "./tables/TableA3",statmat(T3) sdec(3)  rtitle(1*,RE  \ 2*,One-way FE \ 3*,Two-way FE ) ctitle(Model, Estimator, A, "","",B\ "" , "" , DV: Social trust,"","", DV: Political trust,"","") 	addcols(N \"Individuals/observations" \ "`io11'/`io12'" \ "`io21'/`io22'"  \ "`io31'/`io32'"  ) replace tex note("Note: Coefficients with standard errors clustered on individuals in parenthesis and p values in brackets (two-sided tests)") fragment multicol(1,3,3;1,6,3)


*********Figure 1******************
frame copy replication_main temp,replace
cwf temp

keep id poltrust socialtrust WaveNum
reshape wide poltrust socialtrust, i(id) j(WaveNum)

sem (poltrust2 <- poltrust1@b1 socialtrust1@b2 Alpha1@1 ) ///
    (poltrust3 <- poltrust2@b1 socialtrust2@b2 Alpha1@1 ) ///
    (poltrust4 <- poltrust3@b1 socialtrust3@b2 Alpha1@1 ) ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (poltrust18 <- poltrust17@b1 socialtrust17@b2 Alpha1@1 ) ///
    (poltrust19 <- poltrust18@b1 socialtrust18@b2 Alpha1@1 ) ///
    (poltrust20 <- poltrust19@b1 socialtrust19@b2 Alpha1@1 ) ///
    (socialtrust2 <- socialtrust1@b3 poltrust1@b4 Alpha2@1 ) ///
	 (socialtrust3 <- socialtrust2@b3 poltrust2@b4 Alpha2@1 ) ///
    (socialtrust4 <- socialtrust3@b3 poltrust3@b4 Alpha2@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
    (socialtrust18 <- socialtrust17@b3 poltrust17@b4 Alpha2@1 ) ///
    (socialtrust19 <- socialtrust18@b3 poltrust18@b4 Alpha2@1 ) ///
    (socialtrust20 <- socialtrust19@b3 poltrust19@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog  ///
	  cov(socialtrust1*poltrust1 socialtrust1*Alpha2 poltrust1*Alpha1 Alpha1*Alpha2) 

mat A = 8,1,e(N),.,.
mat A = A\8.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\7.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]
test _b[poltrust2:socialtrust1] = _b[socialtrust2:poltrust1] 

*18	  
	 sem ///
	 (poltrust3 <- poltrust2@b1 socialtrust2@b2 Alpha1@1 ) ///
    (poltrust4 <- poltrust3@b1 socialtrust3@b2 Alpha1@1 ) ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (poltrust18 <- poltrust17@b1 socialtrust17@b2 Alpha1@1 ) ///
    (poltrust19 <- poltrust18@b1 socialtrust18@b2 Alpha1@1 ) ///
	 (socialtrust3 <- socialtrust2@b3 poltrust2@b4 Alpha2@1 ) ///
    (socialtrust4 <- socialtrust3@b3 poltrust3@b4 Alpha2@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
    (socialtrust18 <- socialtrust17@b3 poltrust17@b4 Alpha2@1 ) ///
    (socialtrust19 <- socialtrust18@b3 poltrust18@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust2*poltrust2 socialtrust2*Alpha2 poltrust2*Alpha1 Alpha1*Alpha2) 

mat A = A\7,1,e(N),.,.
mat A = A\7.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\6.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]
	  
*16
	 sem ///
    (poltrust4 <- poltrust3@b1 socialtrust3@b2 Alpha1@1 ) ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (poltrust18 <- poltrust17@b1 socialtrust17@b2 Alpha1@1 ) ///
    (socialtrust4 <- socialtrust3@b3 poltrust3@b4 Alpha2@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
    (socialtrust18 <- socialtrust17@b3 poltrust17@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust3*poltrust3 socialtrust3*Alpha2 poltrust3*Alpha1 Alpha1*Alpha2) 

mat A = A\6,1,e(N),.,.
mat A = A\6.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\5.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]


*14	  
	 sem ///
    (poltrust5 <- poltrust4@b1 socialtrust4@b2 Alpha1@1 ) ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (poltrust17 <- poltrust16@b1 socialtrust16@b2 Alpha1@1 ) ///
    (socialtrust5 <- socialtrust4@b3 poltrust4@b4 Alpha2@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
    (socialtrust17 <- socialtrust16@b3 poltrust16@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust4*poltrust4 socialtrust4*Alpha2 poltrust4*Alpha1 Alpha1*Alpha2) 

mat A = A\5,1,e(N),.,.
mat A = A\5.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\4.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]

	  
*12	  
	 sem ///
    (poltrust6 <- poltrust5@b1 socialtrust5@b2 Alpha1@1 ) ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (poltrust16 <- poltrust15@b1 socialtrust15@b2 Alpha1@1 ) ///
    (socialtrust6 <- socialtrust5@b3 poltrust5@b4 Alpha2@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
    (socialtrust16 <- socialtrust15@b3 poltrust15@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust5*poltrust5 socialtrust5*Alpha2 poltrust5*Alpha1 Alpha1*Alpha2)

mat A = A\4,1,e(N),.,.
mat A = A\4.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\3.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]
	  
	  
*10	  
	 sem ///
    (poltrust7 <- poltrust6@b1 socialtrust6@b2 Alpha1@1 ) ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (poltrust15 <- poltrust14@b1 socialtrust14@b2 Alpha1@1 ) ///
    (socialtrust7 <- socialtrust6@b3 poltrust6@b4 Alpha2@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
    (socialtrust15 <- socialtrust14@b3 poltrust14@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust6*poltrust6 socialtrust6*Alpha2 poltrust6*Alpha1 Alpha1*Alpha2)

mat A = A\3,1,e(N),.,.
mat A = A\3.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\2.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]

	 
*8	  
	 sem ///
    (poltrust8 <- poltrust7@b1 socialtrust7@b2 Alpha1@1 ) ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (poltrust14 <- poltrust13@b1 socialtrust13@b2 Alpha1@1 ) ///
    (socialtrust8 <- socialtrust7@b3 poltrust7@b4 Alpha2@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
    (socialtrust14 <- socialtrust13@b3 poltrust13@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust7*poltrust7 socialtrust7*Alpha2 poltrust7*Alpha1 Alpha1*Alpha2) 
	  
mat A = A\2,1,e(N),.,.
mat A = A\2.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\1.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]

	  
*6	  
	 sem ///
    (poltrust9 <- poltrust8@b1 socialtrust8@b2 Alpha1@1 ) ///
    (poltrust10 <- poltrust9@b1 socialtrust9@b2 Alpha1@1 ) ///
    (poltrust11 <- poltrust10@b1 socialtrust10@b2 Alpha1@1 ) ///
    (poltrust12 <- poltrust11@b1 socialtrust11@b2 Alpha1@1 ) ///
    (poltrust13 <- poltrust12@b1 socialtrust12@b2 Alpha1@1 ) ///
    (socialtrust9 <- socialtrust8@b3 poltrust8@b4 Alpha2@1 ) ///
    (socialtrust10 <- socialtrust9@b3 poltrust9@b4 Alpha2@1 ) ///
    (socialtrust11 <- socialtrust10@b3 poltrust10@b4 Alpha2@1 ) ///
    (socialtrust12 <- socialtrust11@b3 poltrust11@b4 Alpha2@1 ) ///
    (socialtrust13 <- socialtrust12@b3 poltrust12@b4 Alpha2@1 ) ///
	 , var(Alpha1 Alpha2) iterate(250) technique(nr 25 bhhh 25) /// 
	  noxconditional nolog ///
	  cov(socialtrust8*poltrust8 socialtrust8*Alpha2 poltrust8*Alpha1 Alpha1*Alpha2) 
mat A = A\1,1,e(N),.,.
mat A = A\1.1,2,r(table)[1,2],r(table)[5,2],r(table)[6,2]
mat A = A\0.9,3,r(table)[1,9],r(table)[5,9],r(table)[6,9]



clear
svmat A
label define WLB 1 "6 (8-13)" 2 "8 (7-14)" 3 "10 (6-15)" 4 "12 (5-16)" 5 "14 (4-17)" 6 "16 (3-18)" 7 "18 (2-19)" 8 "20 (1-20)", replace
lab val A1 WLB

tw  (rspike A4 A5 A1,horizontal) (scatter A1 A3 if A2==2,mc(black) ms(D)) (scatter A1 A3 if A2==3, mc(black) ms(S)), xlabel(-0.06 (0.02) 0.08) xline(0, lc(black)) legend(label (1 95% CI) label(2 "Political trust") label(3 "Social trust") order(- "Dep. var:" 2 3 1) pos(6) row(1)) yscale(range(0.5 8.5)) ylab(1(1)8, nolab notick) ytick(,notick) ytit("") xtitle(Coefficient) name(coef,replace) graphregion(margin(0 0 0 0)) 

tw (bar A3 A1 if A2==1,horizontal), yscale(range(0.5 8.5))  ytit("Number of waves with specific waves in parentheses") name(hist,replace) legend(off pos(6)) xtit(Number of observations) fxsize(50) graphregion(margin(0 0 8.2 0)) xlab(0 (200) 1200) plotregion(margin(0 3 2 2)) ///
ylab(1 "6 (8-13)" 2 "8 (7-14)" 3 "10 (6-15)" 4 "12 (5-16)" 5 "14 (4-17)" 6 "16 (3-18)" 7 "18 (2-19)" 8 "20 (1-20)")

gr combine hist coef, row(1) graphregion(margin(0 0 0 0)) xsize(70) ysize(40)


graph export "./pics/figure1.pdf",replace
graph export "./pics/figure1.eps",replace

******************************************


***Footnote 1 in SI: Correlation between institutional and political trust***
/*Note that this cannot be calculated, because it requires data that could not be included in the replication data file
frame copy full2 temp,replace
cwf temp
statsby ,by(wave) clear: corr insttrust poltrust
rename rho AvgCorr_PT_IT
su AvgCorr_PT_IT
capture putpdf clear
putpdf begin
 putpdf paragraph
 putpdf text ("Correlation between institutional and political trust"),bold linebreak
 putpdf text ("Average correlation = `r(mean)'")
 putpdf save "./tables/CorrPT_IT" , replace 
*/


**************************************************
****Appendix D "Coverage across panel waves"******
**************************************************

****************Table D1**************************
/*Note that this cannot be calculated, because it requires data that could not be included in the replication data file
frame copy full1 temp,replace
cwf temp

tempvar tv2
egen `tv2' = mode(idate), by(wave)
egen STxPT = rowmiss(socialtrust poltrust)
recode STxPT (0=1) (1 2 = 0)
collapse (count) id (first) WaveNum `tv2' (sum) STxPT  ,by(wave)

rename STxPT valid
lab var valid "Respondents who answered both questions"
lab var id "Respondents"

gen modetime = string(`tv2', "%tmMon,_CCYY")
lab var modetime "Month (modal)"
lab var WaveNum "#"
lab var wave "Wave"


texsave WaveNum wave  modetime id valid using "./tables/timeOLD.tex", ///
replace frag varlabels location(h) marker(coverage) title(Coverage across waves)  align(C C C C C)

frame drop full1
*/



*************Figure D1*************************
frame copy replication_main temp, replace
cwf temp

collapse numofwaves, by(id)

capture frame drop ncum
frame create ncum num Nmin N   
forvalues fff = 1/20 {
	qui: count if numofwaves >=`fff'
	sca nmin = `r(N)'
	qui: count if numofwaves == `fff'
	frame post ncum (`fff') (nmin) (r(N))
}
cwf ncum

twoway (bar N num), xlab(1 5 (5) 20) ylab(0 (100) 1000)   plotregion(margin(2 2 0 2)) graphregion(margin(0 0 0 0)) ytit(Number of respondents) xtit(Number of waves with valid responses to both questions) name(cov1,replace) ysize(3) 
graph export "./pics/coverage1.pdf",replace


*************Figure D2**************************
frame copy replication_main temp,replace
cwf temp
loc min = 8
loc max = 13
loc i = 1
forvalues val = 6(2)20 {
	tempvar tvcon`val' tv2con`val'
	gen `tvcon`val'' = 1 if WaveNum >= `min' & WaveNum <= `max'
	egen `tv2con`val'' = sum(`tvcon`val''),by(id)
	gen conanswer`i' = 1 if `tv2con`val'' == `val'
	loc --min
	loc ++max
	loc ++i
}
collapse conanswer*,by(id)
forvalues val = 1/8 {
	egen CA`val' = sum(conanswer`val')
}
keep CA* 
keep in 1
gen ID = 1
reshape long CA,i(ID) j(W)
drop ID

label define WLB 1 "6 (8-13)" 2 "8 (7-14)" 3 "10 (6-15)" 4 "12 (5-16)" 5 "14 (4-17)" 6 "16 (3-18)" 7 "18 (2-19)" 8 "20 (1-20)", replace
lab val W WLB


twoway (bar  CA W,  horizontal), ylab(1(1)8,   valuelabel ) xscale(range(0 1200)) ytit(Waves) xtitle(Number of respondents with valid responses to both questions in all waves) plotregion(margin(0 3 2 2)) graphregion(margin(0 0 0 0)) xlab(0 (300) 1200)  name(continuous,replace) ysize(3)

graph export "./pics/continuous.pdf",replace

**************************************
**Appendix E: Descriptive statistics**

*****Appendix Table E1************
frame copy replication_main temp,replace
cwf temp
xtsum socialtrust if balanced ==1
mat a = `r(mean)' \ `r(sd_b)' \ `r(sd_w)' \ `r(n)' \ `r(N)' \ `r(Tbar)'
xtsum socialtrust // if balanced !=1
mat b = `r(mean)' \ `r(sd_b)' \ `r(sd_w)' \ `r(n)' \ `r(N)' \ `r(Tbar)'
xtsum poltrust if balanced ==1
mat c = `r(mean)' \ `r(sd_b)' \ `r(sd_w)' \ `r(n)' \ `r(N)' \ `r(Tbar)'
xtsum poltrust // if balanced !=1
mat d = `r(mean)' \ `r(sd_b)' \ `r(sd_w)' \ `r(n)' \ `r(N)' \ `r(Tbar)'
mat E = a,b,c,d
mat rownames E = Mean "SD(between)" SD(within) Respondents Observations T-bar

frmttable using "./tables/xtsum", statmat(E) sdec(2 \ 2 \ 2 \ 0  \ 0 \ 2) ctitles("Measure","Social trust","","Political trust" \"Panel sample", Balanced, Unbalanced, Balanced, Unbalanced) multicol(1,2,2;1,4,2) tex replace sfmt(f) fragment 


****Figure E1 + E2
/*Note that these figures cannot be replicated, because they require data that could not be included in the replication data file
frame copy full2 temp,replace
cwf temp
drop if poltrust ==. | socialtrust==.
tempfile tf1
save `tf1',replace
keep id numofwaves
collapse numofwaves,by(id)
keep if numofwaves ==20
set seed 12345
sample 30, count
merge 1:m id using `tf1', keep(3)
xtset id modetime
xtline socialtrust, overlay legend(off) ytit(Social Trust) xtit(Timing of survey (modal month)) note("Note: The figure is based on a random sample of 30 respondents from the balanced panel sample""20 waves/600 observations", placement(center) justification(center))
graph export "./pics/STwithinovertime.pdf",replace
xtline poltrust, overlay legend(off) ytit(Political Trust) xtit(Timing of survey (modal month)) note("Note: The figure is based on a random sample of 30 respondents from the balanced panel sample""20 waves/600 observations", placement(center) justification(center))
graph export "./pics/PTwithinovertime.pdf",replace
*/


***Figure E3***
/*Note that this figure cannot be replicated, because it require data that could not be included in the replication data file
frame copy full2 temp,replace
cwf temp


egen ptub = mean(poltrust), by(WaveNum)
egen ptb = mean(poltrust) if numofwaves ==20, by(WaveNum)
egen stub = mean(socialtrust), by(WaveNum)
egen stb = mean(socialtrust) if numofwaves ==20, by(WaveNum)


collapse  pt* st* modetime,by(wave)
format modetime %tmMon,_CCYY

tw (connected stb modetime,lc(blue) mc(blue)) (connected stub modetime,lc(cyan) mc(cyan)) (connected ptb modetime,lc(red) mc(red)) (connected ptub modetime,lc(magenta) mc(magenta)) , ytit(Average trust) yscale(range(0.4 0.75)) ylab(0.4 (0.1) 0.7) legend(label(1 "Balanced panel sample")label(2 "Unbalanced panel sample") label(3 "Balanced panel sample") label(4 "Unbalanced panel sample") order(- "Social Trust:" 1 2 - "Political trust:" 3 4) pos(6) row(2)) ysize(3.75) xtit(Timing of survey (modal month)) note("Notes: 20 Waves""Balanced panel sample: 297 individuals/5940 observations (T-bar = 20)""Unbalanced panel sample: 9287 individuals/75163 observations (T-bar = 8.09)", placement(center) justification(center)) name(overtime,replace) plotregion(margin(2 2 2 0)) graphregion(margin(0 3 0 0))
graph export "./pics/meanovertime.pdf",replace

*/





*************Generalizability**
*************Figure F1+F2*************************
frame copy replication_main temp,replace
cwf temp

mat resul = .,.,.,.,.,.,.,.
forval i = 1/20 {
	reg socialtrust i.balanced if WaveNum==`i', robust 
	mat sd =     `i',1,r(table)[1,2], r(table)[5,2], r(table)[6,2]
	margins balanced 
	mat snobal = `i'+0.1,2,r(table)[1,1], r(table)[5,1], r(table)[6,1]
	mat sbal   = `i'-0.1,3,r(table)[1,2], r(table)[5,2], r(table)[6,2]
	mat s = sd\snobal\sbal
	reg poltrust i.balanced if WaveNum==`i', robust 
	mat pd =     r(table)[1,2], r(table)[5,2], r(table)[6,2]
	margins balanced 
	mat pnobal = r(table)[1,1], r(table)[5,1], r(table)[6,1]
	mat pbal   = r(table)[1,2], r(table)[5,2], r(table)[6,2]
	mat p = pd\pnobal\pbal
	mat t = s,p
	mat resul = resul\t
}
clear
svmat resul
drop in 1


gen byte test = 0
replace test = 1 if resul2 == 1
 

tw (rspike resul4 resul5 resul1 if resul2==2,lc(blue)) (scatter resul3 resul1 if resul2==2,mc(blue)) ///
(rspike resul4 resul5 resul1 if resul2==3,lc(cyan)) (scatter resul3 resul1 if resul2==3,mc(cyan)) ///
(rspike resul4 resul5 resul1 if resul2==1,lc(black)) (scatter resul3 resul1 if resul2==1,mc(black)) ///
, by(test, cols(1) yrescale note("Note: N varies between 1,725 and 5,469 across waves; see Table D1",placement(center))) ylab(,format(%3.2f)) xscale( range(0.7 20.3))  yline(0) ///
subtitle("") xtitle(Wave) graphregion(margin(0 0 0 0)) plotregion(margin(0 0 2 2)) ytit(Social trust) ///
legend(label (5 "95% CI")label (4 "Balanced panel sample") label(2 "Remaining sample") label(6 "Difference") order(4 2 6 5) row(1)) ///
xlab(1(1)10 11 "12" 12 "13" 13 "15" 14 "18" 15 "21" 16 "23" 17 "26" 18 "28" 19 "32" 20 "33") name(STmeans,replace)

graph export "./pics/STcomparemeans.pdf",replace

tw (rspike resul7 resul8 resul1 if resul2==2,lc(red)) (scatter resul6 resul1 if resul2==2,mc(red)) ///
(rspike resul7 resul8 resul1 if resul2==3,lc(magenta)) (scatter resul6 resul1 if resul2==3,mc(magenta)) ///
(rspike resul7 resul8 resul1 if resul2==1,lc(black)) (scatter resul6 resul1 if resul2==1,mc(black)) ///
, by(test, cols(1) yrescale note("Note: N varies between 1,725 and 5,469 across waves; see Table D1",placement(center))) ylab(,format(%3.2f)) xscale( range(0.7 20.3))  yline(0) ///
subtitle("") xtitle(Wave) graphregion(margin(0 0 0 0)) plotregion(margin(0 0 2 2)) ytit(Political trust) ///
legend(label (5 "95% CI")label (4 "Balanced panel sample") label(2 "Remaining sample") label(6 "Difference") order(4 2 6 5) row(1)) ///
xlab(1(1)10 11 "12" 12 "13" 13 "15" 14 "18" 15 "21" 16 "23" 17 "26" 18 "28" 19 "32" 20 "33") name(PTmeans,replace)
graph export "./pics/PTcomparemeans.pdf",replace


*************Figure F3*************************

frame copy replication_main temp,replace
cwf temp
mat resul = .,.,.,.,.
forval i = 1/20 {
	preserve
	keep if WaveNum == `i'
	egen stz = std(socialtrust) if balanced==0
	egen ptz = std(poltrust) if balanced==0
	egen stz1 = std(socialtrust) if balanced==1
	replace stz = stz1 if balanced == 1
	egen ptz1 = std(poltrust) if balanced==1
	replace ptz = ptz1 if balanced == 1
	drop stz1 ptz1
	reg stz ptz if balanced ==0
	est sto ubal
	mat snobal = `i'+0.1,3,r(table)[1,1], r(table)[5,1], r(table)[6,1]
	reg stz ptz if balanced ==1
	est sto bal
	mat sbal = `i'-0.1,2,r(table)[1,1], r(table)[5,1], r(table)[6,1]
	suest bal ubal
	test [bal_mean=ubal_mean]
	mat p = `i',1,r(p),.,.
	mat t = p\sbal\snobal
	mat resul = resul\t
	restore
}
clear
svmat resul
drop in 1


tw (scatter resul3 resul1 if resul2==2,mc(black)) (rspike resul4 resul5 resul1 if resul2==2,lc(mint)) (scatter resul3 resul1 if resul2==2,mc(mint)) ///
(rspike resul4 resul5 resul1 if resul2==3,lc(green)) (scatter resul3 resul1 if resul2==3,mc(green)) ///
, xscale(off range(0.7 20.3)) xlab(1(1)20, nolab) ylab(,format(%3.2f))  ytit(Pearson's r) name(PR,replace) ///
legend(label (3 "Balanced panel sample") label(5 "Remaining sample") label (4 "95% CI") label (1 "p value from test of difference") off order(3 5 4 1) row(1)) graphregion(margin(0 0 2 0)) plotregion(margin(2 2 2 2)) 
 
tw (scatter resul3 resul1 if resul2==1,mc(black)) ,yline(0.05, lc(black))  xscale(range(0.7 20.3)) xlab(1(1)10 11 "12" 12 "13" 13 "15" 14 "18" 15 "21" 16 "23" 17 "26" 18 "28" 19 "32" 20 "33") ylab(0.05 (0.1) 0.75, format(%3.2f)) xtit(Wave) ytit(p value) name(pv,replace) legend(off) graphregion(margin(0 0 0 0)) fysize(33) yline(0,lp(solid) lc(black)) yscale(range (0 0.8)) plotregion(margin(2 2 0 0))

grc1leg  PR pv, col(1) graphregion(margin(0 0 0 0)) note("Note: N varies between 1,725 and 5,469 across waves; see Table A1",placement(center))
graph export "./pics/comparepearson.pdf",replace


****Figure F4********
frame copy replication_ess temp,replace
cwf temp

capture frame drop correl
frame create correl str2 cntry str20 cround corr ul ll pval N
levelsof name,local(NAME)
foreach N of local NAME {
frame copy replication_ess temp,replace
cwf temp
keep if "`N'"==name
qui: reg  ppltrst trstplt 
keep if e(sample)
egen stz = std(ppltrst)
egen ptz = std(trstplt)
reg  stz ptz
mat b = r(table)
frame post correl (cntry) ("`N'") (b[1,1]) (b[5,1]) (b[6,1]) (b[4,1]) (e(N))
}

frame copy replication_ess temp,replace
cwf temp
encode name,gen(CR)
qui: reg  ppltrst trstplt [pweight=pweight]
keep if e(sample)
egen stz = std(ppltrst)
egen ptz = std(trstplt)
reg  stz ptz [pweight=pweight], cluster(CR)
mat b = r(table)
frame post correl ("") ("Overall") (b[1,1]) (b[5,1]) (b[6,1]) (b[4,1]) (e(N))

frame copy correl temp,replace
cwf temp
encode cround,gen(CR) label(lb)

gsort -corr
gen rank=_n
labmask rank, val(CR) lblname(lb2) decode
drop CR
su rank
loc half = (`r(max)' +1)/2
gen half = 1
replace half = 0 if rank >`half'

sum pval,d
count
sum N,d


levelsof rank if cntry=="SE"  & half==1				, local(blues1)
levelsof rank if cntry!="SE"  & cntry!="" & half==1	, local(blacks1)
levelsof rank if cntry==""  & half==1				, local(reds1)
levelsof rank if half==0							, local(blacks0)



twoway 	(rspike ul ll rank if half==1 & cntry!="SE",  msize(small) horizontal  ) ///
		(rspike ul ll rank if half==1 & cntry=="SE",  msize(small) horizontal color(blue)) ///
		(rspike ul ll rank if half==1 & cntry==""  ,  msize(small) horizontal color(red)) ///
		(scatter rank corr if half==1 & cntry!="SE", msymbol(o) msize(small) mcolor(black)) ///
		(scatter rank corr if half==1 & cntry=="SE", msymbol(o) msize(small) mcolor(blue)) ///
		(scatter rank corr if half==1 & cntry==""  , msymbol(o) msize(small) mcolor(red)), ///
legend(off) ysize(10) ytit("") xlab(0(0.1)0.5)  name(h1,replace) ///
ylabel(none, valuelabel nogrid) ///
ylab(`blues1', add labcolor(blue)) ///
ylab(`blacks1', add custom labcolor(black)) ///
ylab(`reds1', add custom labcolor(red)) ///
xtit(Pearson's r)

twoway 	(rspike ul ll rank if half==0 & cntry!="SE",  msize(small) horizontal  ) ///
		(rspike ul ll rank if half==0 & cntry=="SE",  msize(small) horizontal color(blue) ) ///
		(scatter rank corr if half==0 & cntry!="SE", msymbol(o) msize(small) mcolor(black)) ///
		(scatter rank corr if half==0 & cntry=="SE", msymbol(o) msize(small) mcolor(blue)), ///
legend(off) ysize(10) ytit("") xlab(0(0.1)0.5)  name(h0,replace) ///
ylabel(none, valuelabel nogrid) ///
ylab(`blacks0', add custom labcolor(black)) ///
xtit(Pearson's r)

count if cntry!=""
loc NCW = r(N)
su N if cntry!=""
loc min = r(min)
loc max = r(max)
loc mean = round(`r(mean)',1)
su pval
su N if cntry==""
loc oamean = r(mean)

gr combine h0 h1, rows(1) ysize(8) imargin(0 0 0 0) note("Notes: N{sub:Country-Waves} = `NCW' ; N{sub:Individuals} varies across Country-Waves (`min'-`max'; Mean=`mean')""All p values < 0.001""The overall correlation is based on respondents from all Country-Waves (N = `oamean')""weighted by country population size", size(vsmall)  justification(center) place(6))
graph drop  h0 h1

graph export "./pics/comparepearsonCW.pdf", replace


*Closing
frames reset
clear all
exit
